﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
//using EallNum.Helper;
using System;

//文件处理类
public class FileUtils {

    private static FileUtils _instance;

    public static FileUtils Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new FileUtils();
            }
            return _instance;
        }
    }

    public bool CheckFileExist(string name)
    {
        string path = GetFilePath();
        FileInfo t = new FileInfo(path + "/" + name);
        if (t.Exists)
        {
            return true;
        }
        return false;
    }


    public string GetFilePath()
    {
        string path = Application.persistentDataPath + "/StreamFile";
        DirectoryInfo t = new DirectoryInfo(path);
        if (!t.Exists)
        {
            t.Create();
        }
        return path;
    }

    public void CreateFile(string name, byte[] info)
    {
        string path = GetFilePath();
        Stream sw;
        FileInfo t = new FileInfo(path + "/" + name);
        if (t.Exists)
        {
            t.Delete();
        }
        t = new FileInfo(path + "/" + name);
        sw = t.Create();
        sw.Write(info, 0, info.Length);
        sw.Close();
        sw.Dispose();
    }

    public void DeleteFile(string name)
    {
        string path = GetFilePath();
        FileInfo t = new FileInfo(path + "/" + name);
        if (t.Exists)
        {
            t.Delete();
        }
    }

    /**
* 读取文本文件
   * path：读取文件的路径
  * name：读取文件的名称xxx.txt
  */
    public string[] LoadFile(string name)
    {
        string path = GetFilePath();
        StreamReader sr = null;
        try
        {
            sr = File.OpenText(path + "//" + name);
        }
        catch (Exception e)
        {
            return null;
        }
        string[] arrlist = null;
        string context = sr.ReadToEnd();
        //context = EncrytpHelper.DesDecrypt(context);
        //while ((line = sr.ReadLine()) != null)
        //{            
        //    UTF8Encoding utf8 = new UTF8Encoding();
        //    line = utf8.GetString(utf8.GetBytes(line));
        //    arrlist.Add(line);
        //}

        arrlist = context.Split(new string[] { "LineEnd" }, StringSplitOptions.RemoveEmptyEntries);
        sr.Close();
        sr.Dispose();
        return arrlist;
    }

}
